﻿(function($) {

	var hash = window.location.hash, start = new Date();

	$(function() {
		window.mainpage.instMainMenus();
		window.mainpage.instFavoMenus();
		// window.mainpage.instTimerSpan();
		window.mainpage.bindNavTabsButtonEvent();
		window.mainpage.bindToolbarButtonEvent();
		window.mainpage.bindMainTabsButtonEvent();

		// window.donate.reload();
		// window.link.reload();

		var portal = $("#portal"), layout = $("#mainLayout"), navTabs = $("#navTabs"), themeCombo = $("#themeSelector");

		$.util.exec(function() {
			var theme = $.easyui.theme(), themeName = $.cookie("themeName");
			if (themeName && themeName != theme) {
				window.mainpage.setTheme(themeName, false);
			}

			layout.removeClass("hidden").layout("resize");

			$("#maskContainer").remove();

			var size = $.util.windowSize();
			// 判断当浏览器窗口宽度小于像素 1280 时，右侧 region-panel 自动收缩
			if (size.width < 1280) {
				layout.layout("collapse", "east");
			}

			window.mainpage.mainTabs.loadHash(hash);

			var stop = new Date();
			/*
			 * $.easyui.messager.show({ msg : "当前页面加载耗时(毫秒)：" +
			 * $.date.diff(start, "ms", stop), position : "bottomRight" });
			 */
		});

		$("#A1").click(function() {
			portal.portal("addColumn");
		});
		$("#A2").click(function() {
			portal.portal("removeColumn", portal.portal("columns") - 1);
		});

		$("#A3").click(function() {
			$.easyui.loading();
			$.util.exec(function() {
				$.easyui.loaded();
			}, 1000);
		});

		$("#A4").click(function() {
			// $.easyui.showDialog({
			// href: "http://www.baidu.com", iniframe: true
			// });
			var str = "jqext/common/index-startup.js", url = $.util.resolveUrl(str);
			window.mainpage.util.getUrlResponse(url, function(text) {
				alert(text);
			});
		});

		$("#btn3").click(function() {
			var t = $("#navMenu_Tree"), node = t.tree("getNode", $("div[node-id=1107]")[0]);
			t.tree("setText", {
				target : node.target,
				text : "icon-ok"
			});
		});

		$.util.namespace("mainpage.util");
		/*
		 * window.mainpage.util.getUrlResponse = function(url, callback) {
		 * $.get("./tests/HttpUtility.asmx/GetUrlResponse", { url : url },
		 * function(data) { var text = $(data).text(); if
		 * ($.isFunction(callback)) { callback.call(this, text); } }); };
		 */

		// 主页中的待办任务条数
		$.get(kl.ctx + "/bpm/workspace/workspace-listPersonalTasks-count", function(data) {
			if (data.success) {
				var $unfinishedTask = $("<a><span class='unfinishedTask_count'>" + data.obj + "</span></a>").prependTo("#buttonbar")
						.linkbutton({
							iconCls : "icon-cologne-basket",
							plain : true
						}).click(function() {
							window.mainpage.mainTabs.addModule("我的待办任务", kl.ctx + "/bpm/workspace/to-workspace-listPersonalTasks");
						}).tooltip({
							position : "top",
							content : "您有" + data.obj + "条待办任务"
						});
				if (data.obj > 0) {
					$unfinishedTask.tooltip("show");
				}
			}
		}, "json");
		// 主页中的我的未读邮件条数
		$.get(kl.ctx + "/basic/notifyInfo/receiverNotify-count", function(data) {
			if (data.success) {
				var $unfinishedTask = $("<a><span class='unfinishedTask_count'>" + data.obj + "</span></a>").prependTo("#buttonbar")
						.linkbutton({
							iconCls : "icon-hamburg-contact",
							plain : true
						}).click(function() {
							window.mainpage.mainTabs.addModule("收件箱", kl.ctx + "/basic/notifyInfo/to_receiver");
						}).tooltip({
							content : "您有" + data.obj + "条未读邮件"
						});
				if (data.obj > 0) {
					$unfinishedTask.tooltip("show");
				}
			}
		}, "json");
		// 主页中的日程数（结束时间大于现在）
		$.get(kl.ctx + "/basic/calendar/calendar-count", function(data) {
			if (data.success) {
				var $unfinishedTask = $("<a><span class='unfinishedTask_count'>" + data.obj + "</span></a>").prependTo("#buttonbar")
						.linkbutton({
							iconCls : "icon-cologne-calendar",
							plain : true
						}).click(function() {
							window.mainpage.mainTabs.addModule("我的日程管理", kl.ctx + "/basic/calendar");
						}).tooltip({
							position : "top",
							content : "您有" + data.obj + "条日程安排"
						});
				if (data.obj > 0) {
					$unfinishedTask.tooltip("show");
				}
			}
		}, "json");
		// portal中常用事务流程
		var $commonProcessPortalDiv = $("#commonProcessPortalDiv");
		$.get(kl.ctx + "/bpm/process/commonProcess", function(data) {
			$.each(data, function(index, value) {
				$("<a></a>").appendTo($commonProcessPortalDiv).linkbutton({
					text : value.name,
					iconCls : "icon-cologne-32-limited-edition",
					size : 'large',
					iconAlign : "top"
				}).click(
						function() {
							window.mainpage.mainTabs.addModule("[" + value.name + "]流程配置", kl.ctx
									+ "/bpm/confForm/form-viewStartForm?bpmProcessId=" + value.id);
						}).linkbutton("setStyle", {
					margin : 10
				});
				;
			});
		}, "json");
		// portal中待办任务
		var $todoPortalDiv = $("#todoPortalDiv");
		var $toClaimPortalDiv = $("#toClaimPortalDiv");
		var taskConfig = {
			method : "get",
			url : kl.ctx + "/bpm/workspace/workspace-listPersonalTasks",
			idField : 'id',
			remoteSort : true,
			multiSort : true,
			rownumbers : true,
			border : false,
			fit : true,
			singleSelect : true,
			pagination : true,
			pageSize : 5,
			pageList : [ 5, 10, 20, 30, 40, 50, 100 ],
			columns : [ [
					{
						field : 'pdname',
						title : '业务名称'
					},
					{
						field : 'name',
						title : '任务名称',
						formatter : function(value, row, index) {
							return "<a class='name-btn' onclick='graphTrace(this);'  href='#' pid='" + row.pid + "' title='点击查看流程图'>"
									+ value + "</a>";
						}
					},
					{
						field : 'createTime',
						title : '创建时间'
					},
					{
						field : 'action',
						title : '操作',
						formatter : function(value, row, index) {
							return "<a class='banli-btn' href='#' onclick='window.mainpage.mainTabs.addModule(\"[" + row.name
									+ "]任务办理\",\"" + kl.ctx + "/bpm/confForm/form-viewHandleTaskPage?taskId=" + row.id + "\")'>办理</a>";
						}
					}, {
						field : 'pid',
						title : '版本',
						formatter : function(value, row, index) {
							return "V:" + row.pdversion
						}
					} ] ],
			enableHeaderClickMenu : true, // 此属性开启表头列名称右侧那个箭头形状的鼠标左键点击菜单
			enableHeaderContextMenu : true, // 此属性开启表头列名称右键点击菜单
			enableRowContextMenu : false,
			onLoadSuccess : function(data) {
				$('.banli-btn').linkbutton({
					iconCls : 'icon-hamburg-document'
				});
				$(".qianshou-btn").linkbutton({
					iconCls : 'icon-hamburg-business'
				});
				$(".name-btn").tooltip();
			}
		};
		var $todogrid = $("<div id='todotask-datagrid'></ol>").appendTo($todoPortalDiv).datagrid(taskConfig);
		// portal中待签收任务
		var toClaimTaskConfig = $.extend(taskConfig, {
			url : kl.ctx + "/bpm/workspace/workspace-listGroupTasks",
			columns : [ [
					{
						field : 'pdname',
						title : '业务名称'
					},
					{
						field : 'name',
						title : '任务名称',
						formatter : function(value, row, index) {
							return "<a class='name-btn' onclick='graphTrace(this);'  href='#' pid='" + row.pid + "' title='点击查看流程图'>"
									+ value + "</a>";
						}
					},
					{
						field : 'createTime',
						title : '创建时间'
					},
					{
						field : 'action',
						title : '操作',
						formatter : function(value, row, index) {
							return "<a class='qianshou-btn' href='#' onclick='cliamTask(\"" + row.id
									+ "\",\"#todotask-datagrid\",\"#toclaimtask-datagrid\")'>签收</a>";
						}
					}, {
						field : 'pid',
						title : '版本',
						formatter : function(value, row, index) {
							return "V:" + row.pdversion
						}
					} ] ]
		});
		var $claimgrid = $("<div id='toclaimtask-datagrid'></ol>").appendTo($toClaimPortalDiv).datagrid(taskConfig);

		// 身份切换
		var $userSelector = $("#userId").comboselector($.extend(userSelectOpts, {
			url : kl.ctx + "/basic/user/runAsdataGrid"
		}));
		$("#btnRunAs").click(function() {
			$("#fm").form("clear");
			$("#dlg").dialog("open").dialog("setTitle", "身份切换");
		});
		$("#btnSwithBack").click(function() {
			$.get(kl.ctx + "/basic/user/switchBack", function(data) {
				$.messager.show({
					msg : data.msg,
					icon : "info",
					position : "bottomRight"
				});
				location.reload(true);
			}, "json")
		});
		$("#companyTypeSelector").combobox({
			valueField : 'id',
			textField : 'text',
			method : 'get',
			width : 140,
			url : kl.ctx + '/entersale/essCompanyType/comboBoxData',
			onSelect : function(record) {
				window.parent.location.href = kl.ctx + '/index?companyType=' + record.id;
			}
		});
	});

})(jQuery);
function fn_runAs() {
	var userId = $("#userId").comboselector("getValue");
	$.get(kl.ctx + "/basic/user/runAs?userId=" + userId, function(data) {
		$.messager.show({
			msg : data.msg,
			icon : "info",
			position : "bottomRight"
		});
		location.reload(true);
	}, "json")
}